/*
 * Copyright 2013 Insu Song. All rights reserved.
 * inssong@gmail.com  http://www.kopo.com  
 * 
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 * 
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 * 
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARSHAN POURSOHI OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied.
 */
package com.kopo.droidioiolib;

import com.kopo.droidioiolib.Behavior.RunUI;

import ioio.lib.util.IOIOLooper;
import ioio.lib.util.IOIOLooperProvider;
import ioio.lib.util.android.IOIOAndroidApplicationHelper;
import android.app.Application;
import android.util.Log;

// An entity that provides IOIOLooper instances on demand, per connection specifications.
// Based on the idea posted on
//   https://groups.google.com/forum/#!msg/ioio-users/PlalZY9qse0/od59eiBM2TgJ
public class ControlDevice extends Application implements IOIOLooperProvider {
    public static Boolean connStatus=false;
    public static ControlDevice me = null;
    public static ControlInterface controlInterface = null;
    public static Behavior currentBehavior = null;
    public static RunUI currentActivity = null;
    private static final String TAG = "kopo.log";

    // Share IOIOAndroidApplicationHelper across activities
    private final IOIOAndroidApplicationHelper helper_ = new IOIOAndroidApplicationHelper(this, this);

    // Call this to create an IOIOActivity
    public void create()
    {
    	Log.d(TAG, "ControlDevice.create");

        helper_.create();
    }

    public void destroy() 
    {
    	Log.d(TAG, "ControlDevice.destroy");
        helper_.destroy();
    }

    public void start() 
    {
    	Log.d(TAG, "ControlDevice.start");
        helper_.start();
    }

    public void stop() 
    {
    	Log.d(TAG, "ControlDevice.stop");
		if(ControlDevice.me != null)
		{
			if( ControlDevice.currentBehavior != null)
				ControlDevice.currentBehavior.enabled = false;
		}
        helper_.stop();
        
    }

    public void restart()
    {
    	Log.d(TAG, "ControlDevice.restart");
        helper_.restart();
    }

    // Called when an IOIO device is connected to create a thread (IOIOLooper)
    // We can look at the connection information to decide whether to create a thread for the device.
	@Override
	public IOIOLooper createIOIOLooper(String arg0, Object arg1) {
    	Log.d(TAG, "ControlDevice.createIOIOLooper");

		ControlDevice.controlInterface = new ControlInterface();  // create a thread handler
		ControlInterface.me = ControlDevice.controlInterface;
		return ControlDevice.controlInterface;  // IOIOLooperProvider will create a thread and call theInterface.loop() in the thread
	}

}
